package cn.mastor.basic.util;

import cn.mastor.order.domain.OrderPetAcquisition;
import cn.mastor.order.domain.OrdercancelLog;
import cn.mastor.order.mapper.OrderPetAcquisitionMapper;
import cn.mastor.order.mapper.OrdercancelLogMapper;
import cn.mastor.pet.mapper.SearchMasterMsgMapper;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "hello_xhr_topic", topic = "hello_topic")
    public class SyncMessageListener implements RocketMQListener<String> {
        @Autowired
        OrderPetAcquisitionMapper orderPetAcquisitionMapper;
        @Autowired
        OrdercancelLogMapper ordercancelLogMapper;
        @Override
        public void onMessage(String s) {
            OrderPetAcquisition orderPetAcquisition=orderPetAcquisitionMapper.queryByOrdersn(s);
            if (null!=orderPetAcquisition&&orderPetAcquisition.getState()==0){
                orderPetAcquisition.setShopId(null);
                System.out.println("过期删除"+orderPetAcquisition);
                OrdercancelLog ordercancelLog = new OrdercancelLog();
                ordercancelLog.setAcId(Math.toIntExact(orderPetAcquisition.getId()));
                ordercancelLog.setNote("订单由于未及时处理，于"+new Date()+"自动取消");
                ordercancelLog.setTime(new Date());
                ordercancelLogMapper.add(ordercancelLog);
                orderPetAcquisition.setState(-1);
                orderPetAcquisitionMapper.update(orderPetAcquisition);
            }
            log.info("消费到了同步消息: message = {}", s);
        }
    }